package com.wjb.xietong.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wjb.xietong.R;

public class SelectPicPopupWindow extends PopupWindow {

	private TextView btn_delete = null;
	private TextView btn_edit = null;
	private TextView btn_finish_task = null;
	private TextView btn_cancel = null;
	private View mMenuView;
	
	public SelectPicPopupWindow(Activity context, boolean needCancelFinish, OnClickListener itemsOnClick) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater
				.inflate(R.layout.page_task_detail_pop_window, null);
		btn_delete = (TextView) mMenuView.findViewById(R.id.btn_delete);
		btn_edit = (TextView) mMenuView.findViewById(R.id.btn_edit);
		btn_finish_task = (TextView) mMenuView
				.findViewById(R.id.btn_finish_task);
		btn_cancel = (TextView) mMenuView.findViewById(R.id.btn_cancel);
		
		if(needCancelFinish){
			btn_finish_task.setText("取消完成");
		}
		// 设置按钮监听
		btn_delete.setOnClickListener(itemsOnClick);
		btn_edit.setOnClickListener(itemsOnClick);
		btn_finish_task.setOnClickListener(itemsOnClick);
		btn_cancel.setOnClickListener(itemsOnClick);
		// 设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimBottom);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {

				int height = mMenuView.findViewById(R.id.pop_layout).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});

	}

}
